package com.newlink.building.userinfo.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.ext.setNewOrAddData
import com.newlink.building.common_base.model.bean.FamilyManagementListData1
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.adapter.Module_UserInfo_FamilyManagementAdapter
import com.newlink.building.userinfo.databinding.ActivityFamilyManagementBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_FamilyManagementPresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_FamilyManagementContract

/**
 * @author wangyang
 * @date 2022/9/16
 * @description 我的——家庭管理页面
 */
class Module_UserInfo_FamilyManagementActivityNL :
    NL_BaseMvpActivity<Module_UserInfo_FamilyManagementContract.View, Module_UserInfo_FamilyManagementContract.Presenter>(),
    Module_UserInfo_FamilyManagementContract.View, View.OnClickListener {

    private var curList: MutableList<FamilyManagementListData1> = ArrayList()
    var mAdapter: Module_UserInfo_FamilyManagementAdapter? = null
    var curPage: Int = 1
    var curLimit: Int = 20
    private var emptyContent: View? = null

//    override fun attachLayoutRes(): Int {
//        return R.layout.activity_family_management
//    }

    private lateinit var mBinding: ActivityFamilyManagementBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityFamilyManagementBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
    }

    override fun initView() {
        super.initView()
        emptyContent = findViewById(R.id.empty_content)
        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.family_management_title)

//        if (isPrimaryAccount) {
//            findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.VISIBLE
//        } else {
//            findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.GONE
//        }

        findViewById<ImageView>(R.id.iv_title_bar_right).setOnClickListener(this)
    }

    fun updateAddUserButton(familySize: Int) {
        if (maxUserCount > familySize) {
            if (isPrimaryAccount) {
                findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.VISIBLE
            } else {
                findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.GONE
            }
        } else {
            findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.GONE
        }
    }

    override fun initRestoreInstanceState(savedInstanceState: Bundle) {
        finish()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_title_bar_right -> {
                //添加家庭成员
                startActivity(Intent(this, Module_UserInfo_FamilyManagementAddActivityNL::class.java))
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mPresenter?.getFamilyMaxMemberCount(
            accessToken,
            masterUserId = userId,
            prjType = userInfoData.prjType
        )

    }

    private fun initAdapter(list: MutableList<FamilyManagementListData1>) {

        curList.clear()
        curList = list

        val tempList = ArrayList<FamilyManagementListData1>()
        for (temp in curList) {
            if (temp.id != "$userId") {
                tempList.add(temp)
            } else {
                NLog.e("家庭列表发现自己,跳过~~")
            }
        }

        if (mAdapter == null) {
            mAdapter = Module_UserInfo_FamilyManagementAdapter(tempList)
            mAdapter?.addChildClickViewIds(
                R.id.cl
            )
            mAdapter?.setOnItemChildClickListener { adapter, view, position ->
                val status = tempList[position].appStatus
                when (view.id) {
                    R.id.cl -> {
                        when (status) {
                            1 -> {
                                //等待激活
                                if (tempList[position].qrFlag) {
                                    //二维码
                                    Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeActivityNL.startSelf(
                                        this,
                                        tempList[position]
                                    )
                                } else {
                                    //邮箱
                                    Module_UserInfo_FamilyManagementDetailWaitActivationEmailActivityNL.startSelf(
                                        this,
                                        tempList[position]
                                    )
                                }
                            }

                            2 -> {
                                //已激活
                                Module_UserInfo_FamilyManagementDetailActivationActivityNL.startSelf(
                                    this,
                                    tempList[position]
                                )
                            }
                        }
                    }
                }
            }

            mBinding.rvFamilyManagement.adapter = mAdapter
        }
        mAdapter?.setNewOrAddData(true, tempList)

        if (tempList.size == 0) {
            emptyContent?.visibility = View.VISIBLE
            mBinding.rvFamilyManagement.visibility = View.GONE
        } else {
            emptyContent?.visibility = View.GONE
            mBinding.rvFamilyManagement.visibility = View.VISIBLE
        }
    }

    override fun createPresenter(): Module_UserInfo_FamilyManagementContract.Presenter {
        return Module_UserInfo_FamilyManagementPresenter()
    }

    override fun getFamilyManagementListSuccess(list: MutableList<FamilyManagementListData1>) {
        val copyList = list.toMutableList()
        NLog.e("|jake| ...family size . > ${copyList.size}")
        try {
            for (item in copyList) {
                NLog.e("|jake| .... > ${item.id}")
//                if (item.sip == sipInforData.sipAccount) {
//                    copyList.remove(item)
//                }
            }
            copyList.sortBy { it.aid }
            initAdapter(copyList)
        } catch (e: Exception) {
            NLog.e("|jake| ...family exception is  . > $e")
        }
        updateAddUserButton(list.size)
    }

    override fun getFamilyManagementListFail() {
        updateAddUserButton(0)
    }

    override fun getFamilyMaxMemberCountSuccess(count: Int) {
        NLog.e("|jake| ...family max user count is  . > $count")
        maxUserCount = count
        mPresenter?.getFamilyManagementList(accessToken, curPage, curLimit)
    }

    override fun getFamilyMaxMemberCountFail() {
        mPresenter?.getFamilyManagementList(accessToken, curPage, curLimit)
    }
}